<h3>Chapter 6: Variable Handling</h3>
<p><strong>Review</strong></p>
<p>
By now you should be able to code some simple objects
using your muds standard object library.  Inheritance allows you
to use functions defined in those objects without having to go
and define yourself. In addition, you should know how to declare
your own functions. This chapter will teach you about the basic
elements of LPC which will allow you to define your own functions
using the manipulation of variables.
</p>
<hr size="1">
<p><strong>Values and objects</strong></p>
<p>
Basically, what makes objects on the mud different are two things:
<ol>
<li>Some have different functions
<li>All have different values
</ol>
Now, all player objects have the same functions.
They are therefore differentiated by the values they hold. For
instance, the player named &quot;Forlock&quot; is different from
&quot;Descartes&quot; <b>at least </b>in that they have
different values for the variable true_name, those being
&quot;descartes&quot; and &quot;forlock&quot;.
</p>
<p>
Therefore, changes in the game involve changes in
the values of the objects in the game. Functions
are used to name specific process for manipulating values. For
instance, the create() function is the function whose process
is specifically to initialize the values of an object.
</p>
<p>
Within a function, it is specifically things called
instructions which are responsible for the
direct manipulation of variables.
</p>
<hr size="1">
<p><strong>Local and global variables</strong></p>
<p>
Like variables in most programming language, LPC
variables may be declared as variables &quot;local&quot; to a
specific function, or &quot;globally&quot; available to all functions.
Local variables are declared inside the function which will use
them. No other function knows about their existence, since
the values are only stored in memory while that function
is being executed. A global variable is available to any function
which comes after its declaration in the object code. Since global
variables take up RAM for the entire existence
of the object, you should use them only when you need a value
stored for the entire existence of the object.
</p>
<p>
Have a look at the following 2 bits of code:
<pre>
int x;

int query_x() { return x; }

void set_x( int y ) { x = y; }

---

void set_x( int y ) {
  int x;

  x = y;
  write(&quot;x is set to x&quot;+x+&quot; and will now be forgotten.\n&quot;);

}
</pre>
In the first example, x is declared outside of any functions, and
therefore will be available to any function declared after it.
In that example, x is a global variable.
</p>
<p>
In the second example, x is declared inside the function
set_x(). It only exists while the function set_x() is being executed.
Afterwards, it ceases to exist. In that example, x is a local
variable.
</p>
<hr size="1">
<p><strong>Manipulating the values of variables</strong></p>
<p>
Instructions to the driver are used to manipulate
the values of variables. An example of an instruction would be:
<pre>
x = 5;
</pre>
The above instruction is self-explanatory. It assigns
to the variable x the value 5. However, there are some important
concepts in involved in that instruction which are involved in
instructions in general.
</p>
<p>
The first involves the concept of an expression.
An expression is any series of symbols which have a value. In
the above instruction, the variable x is assigned the value of
the expression 5. Constant values are the
simplest forms in which expressions can be put. A constant is
a value that never changes like the int 5 or the string &quot;hello&quot;.
The last concept is the concept of an operator. In the above example,
the assignment operator = is used.
</p>
<p>
There are however many more operators in LPC, and
expressions can get quite complex. If we go
up one level of complexity, we get:
<pre>
y = 5;
x = y + 2;
</pre>
The first instruction uses the assignment operator
to assign the value of the constant expression
5 to the variable y. The second one uses the assignment operator
to assign to x the value of the expression (y+2)
which uses the addition operator to come up with a value which
is the sum of the value of y and the value of the constant expression 2.
</p>
<p>
Sound like a lot of hot air?
</p>
<p>
In another manner of speaking, operators can be used
to form complex expressions. In the above
example, there are two expressions in the one instruction x = y + 2;:
<ol>
<li>the expression y + 2
<li>the expression x = y + 2
</ol>
As stated before, all expressions have a value. The
expression y+2 has the value of the sum of y and 2 (here, 7);
The expression x = y + 2 *also* has the value of 7. So
operators have two important tasks:
<ol>
<li>They <b>may</b> act upon input like a function
<li>They evaluate as having a value themselves.
</ol>
Now, not all operators do what 1 does. The = operators
does act upon the value of 7 on its right
by assigning that value to x. The operator + however does nothing.
They both, however, have their own values.
</p>
<hr size="1">
<p><strong>Complex expressions</strong></p>
<p>
As you may have noticed above, the expression x = 5 <b>itself</b> has
a value of 5. In fact, since LPC operators themselves have value as
expressions, they can allow you to write some really convoluted
looking nonsense like:
<pre>
i = ( ( x = sizeof( tmp = users() ) ) ? --x : sizeof( tmp = children(&quot;/std/monster&quot;) ) -1 )
</pre>
which says basically:
<blockquote>
assing to tmp the array returned by the efun users(),
then assign to x the value equal to the number
of elements to that array. If the value of the expression assigning
the value to x is true (not 0), then assign x
by 1 and assign the value of x-1 to i. If x is false though, then
set tmp to the array returned by the efun children(), and then
assign to i the value of the number of members in the array tmp -1.
</blockquote>
Would you ever use the above statement? I doubt it.
However you might see or use expressions similar
to it, since the ability to consolidate so much information into
one single line helps to speed up the execution of your
code. A more often used version of this property of LPC operators
would be something like:
<pre>
x = sizeof( tmp = users() );
while( i-- )
    write( (string)tmp[i]-&gt;query_name()+&quot;\n&quot;);
</pre>
instead of writing something like:
<pre>
tmp = users();
x = sizeof(tmp);
for( i = 0; i&lt; x; i++ )
    write( (string)tmp[i]-&gt;query_name()+&quot;\n&quot;);
</pre>
Things like for(), while(), arrays and such will
be explained later. But the first bit of code
is more concise and it executed faster.
</p>
<p>
NOTE: A detailed description of all basic LPC operators
follows the chapter summary.
</p>
<hr size="1">
<p><strong>Chapter Summary</strong></p>
<p>
You now know how to declare variables and understand
the difference between declaring and using
them globally or locally. Once you become familiar with your driver's
efuns, you can display those values in many different ways. In
addition, through the LPC operators, you know how to change and
evaluate the values contained in variables. This is useful of
course in that it allows you to do something like count how many
apples have been picked from a tree, so that once all apples have
been picked, no players can pick more. Unfortunately, you do not
know how to have code executed in anything other than a linera
fashion. In other words, hold off on that apple until the next
chapter, cause you do not know how to check if the apples picked
is equal to the number of apples in the tree. You also do not
know about the special function init() where you give new commands
to players. But you are almost ready to code a nice, fairly
complex area.
</p>
<hr size="1">
<p><strong>LPC operators</strong></p>
<p>
This section contains a detailed listing of the simpler
LPC operators, including what they do to the
values they use (if anything) and the value that they have.
</p>
<p>
The operators described here are:
<pre>
= + - * / % += -= *= /= %=
-- ++ == != &gt; &lt; &gt;= &lt;= ! &amp;&amp; ||
-&gt; ? :
</pre>
Those operators are all described in a rather dry
manner below, but it is best to at least look
at each one, since some may not behave <b>exactly</b> as you think.
But it should make a rather good reference guide.

<DL>
  <DT>= assignment operator:
  <DD>
      <dl>
	<dt>example: x = 5;
	<DT>value: the value of the variable on the <B>left</B> after its
	    function is done
	<DT>explanation: It takes the value of any expression on the
	    <B>right</B> and assigns it to the variable on the
	    <B>left</B>. Note that you must use a single variable on the left,
	    as you cannot assign values to constants or complex expressions.
      </dl>

  <dt>+ addition operator:
      <dl>
	<dt>example: x + 7
	<dt>value: The sum of the value on the left and the value on the right
	<dt>explanation: It takes the value of the expression on the right and
	    adds it to the value of the expression on the left. For values of
	    type int, this means the numerical sum. For strings, it means that
	    the value on the right is stuck onto the value on the left
	    (&quot;ab&quot; is the value of &quot;a&quot;+&quot;b&quot;).
	    This operator does not modify any of the original values (i.e. the
	    variable x from above retains its old value).
      </dl>
  <dt>- subtraction operator:
      <dl>
	<dt>example: x - 7
	<dt>value: the value of the expression on the left reduced by the right
	<dt>explanation: Same characteristics as addition, except it subtracts.
	    With strings: &quot;a&quot; is the value of &quot;ab&quot; -
	    &quot;b&quot;
      </dl>
  <dt>* multiplication operator:
      <dl>
	<dt>example: x*7
	<dt>value and explanation: same as with adding and subtracting except
	    this one performs the math of multiplication
      </dl>
  <dt>/ division operator:
      <dl>
	<dt>example: x/7
	<dt>value and explanation: see above
      </dl>
  <dt>+= additive assignment operator:
      <dl>
	<dt>example: x += 5
	<dt>value: the same as x + 5
	<dt>explanation: It takes the value of the variable on the left and
	    the value of the expression on the right, adds them together
	    and assigns the sum to the variable on the left.
	<dt>example: if x = 2... x += 5 assigns the value 7 to the variable
	    x. The whole expression has the value of 7.
      </dl>
  <dt>-= subtraction assignment operator
      <dl>
	<dt>example: x-=7
	<dt>value: the value of the left value reduced by the right value
	<dt>explanation: The same as += except for subtraction.
      </dl>
  <dt>*= multiplicative assignment operator
      <dl>
	<dt>example: x *= 7
	<dt>value: the value of the left value multiplied by the right
	<dt>explanation: Similar to -= and += except for addition.
      </dl>
  <dt>/= division assignment operator
      <dl>
	<dt>example: x /= 7
	<dt>value: the value of the variable on the left divided by the right
	    value
	<dt>explanation: similar to above, except with division
      </dl>

  <dt>++ post/pre-increment operators
      <dl>
	<dt>examples: i++ or ++i
	<dt>values: i++ has the value of i ++i has the value of i+1
	<dt>explanation: ++ changes the value of i by increasing it by
	    1. However, the value of the expression depends on where you
	    place the ++. ++i is the pre-increment operator. This means
	    that it performs the increment <b>before</b> giving a
	    value.
	    i++ is the post-increment operator. It evalutes before
	    incrementing i. What is the point? Well, it does not much
	    matter to you at this point, but you should recognize what it
	    means.

      </dl>
  <dt>-- post/pre-decrement operators
      <dl>
	<dt>examples: i-- or --i
	<dt>values: i-- the value of i --i the value of i reduced by 1
	<dt>explanation: like ++ except for subtraction
      </dl>
  <dt>== equality operator
      <dl>
	<dt>example: x == 5
	<dt>value: true or false (not 0 or 0)
	<dt>explanation: it does nothing to either value, but it returns
	    true if the 2 values are the same. It returns false if they are
	    not equal.

      </dl>
  <dt>!= inequality operator
      <dl>
	<dt>example: x != 5
	<dt>value: true or false
	<dt>explanation returns true if the left expression is not equal to
	    the right expression. It returns fail if they are equal.
      </dl>
  <dt>&gt; greater than operator
      <dl>
	<dt>example: x &gt; 5
	<dt>value: true or false
	<dt>explanation: true only if x has a value greater than false if
	    the value is equal or less
      </dl>


  <dt>&lt; less than operator
  <dt>&gt;= greater than or equal to operator
  <dt>&lt;= less than or equal to operator
      <dl>
	<dt>examples: x &lt; y x &gt;= y x &lt;= y
	<dt>values: true or false
	<dt>explanation: similar as to &gt; except
	    &lt; true if left is less than right
	    &gt;= true if left is greater than <b>or equal to</b>
	    right
	    &lt;= true if the left is less than <b>or equal to</b>
	    the right
      </dl>
  <dt>&amp;&amp; logical and operator
  <dt>|| logical or operator
      <dl>examples: x &amp;&amp; y x || y
	<dt>values: true or false
	<dt>explanation: If the right value and left value are
	    non-zero, &amp;&amp; is true.
	    If either are false, then &amp;&amp; is false.
	    For ||, only one of the values must be true for it
	    to evaluate
	    as true. It is only false if both values indeed
	    are false

      </dl>
  <dt>! negation operator
      <dl>
	<dt>example: !x
	<dt>value: true or false
	<dt>explanation: If x is true, then !x is false
	    If x is false, !x is true.
      </dl>
</dl>

A pair of more complicated ones that are here just for the sake of being
here. Do not worry if they utterly confuse you.

<dl>
  <dt>-&gt; the call other operator
      <dl>
	<dt>example: this_player()-&gt;query_name()
	<dt>value: The value returned by the function being called
	<dt>explanation: It calls the function which is on the
	    right in the object
	    on the left side of the operator. The left expression
	    <b>must</b> be
	    an object, and the right expression *must* be the
	    name of a function. If not such function exists in the object, it
	    will return 0 (or more correctly, undefined).
      </dl>
  <dt> ? : conditional operator
      <dl>
	<dt>example: x ? y : z
	<dt>values: in the above example, if x is try, the value is y if x is
	    false, the value of the expression is z
	<dt>explanation: If the leftmost value is true, it will
	    give the expression as
	    a whole the value of the middle expression. Else,
	    it will give the
	    expression as a whole the value of the rightmost
	    expression.
      </dl>
</dl>

A note on equality: A very nasty error people make that is VERY difficult to
debug is the error of placing = where you mean ==. Since operators return
values, they both make sense when being evaluated.
</p>
<p>
In other words, no error occurs. But they have very different values. For example:
<pre>
if( x == 5 )
if( x = 5 )
</pre>
The value of x == 5 is true if the value of x is 5, false othewise. The value
of x = 5 is 5 (and therefore always true). The if statement is looking for the
expression in () to be either true or false, so if you had = and meant ==, you
would end up with an expression that is always true. And you would pull your
hair out trying to figure out why things were not happening like they should :)
</p>
